package com.forum.module.system.api.user;

import cn.hutool.core.collection.CollUtil;
import com.forum.framework.common.enums.CommonStatusEnum;
import com.forum.framework.common.pojo.DataResult;
import com.forum.framework.common.util.object.BeanUtils;
import com.forum.module.system.api.user.dto.AdminUserRespDTO;
import com.forum.module.system.dal.dataobject.user.UserDO;
import com.forum.module.system.dal.mysql.user.UserMapper;
import jakarta.annotation.Resource;

import java.util.Collection;
import java.util.List;

import static com.forum.framework.common.pojo.DataResult.success;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/3 20:45
 */
public class AdminUserApiImpl implements AdminUserApi{
    @Resource
    private UserMapper userMapper;

    @Override
    public DataResult<AdminUserRespDTO> getUserById(Long id) {
        UserDO userDO = userMapper.selectById(id);
        return success(BeanUtils.toBean(userDO, AdminUserRespDTO.class));
    }

    @Override
    public DataResult<List<AdminUserRespDTO>> getUserList(Collection<Long> ids) {
        List<UserDO> userDOS = userMapper.selectByIds(ids);
        List<AdminUserRespDTO> userRespDTOS = userDOS.stream()
                .map(userDO -> {
                    AdminUserRespDTO dto = new AdminUserRespDTO();
                    BeanUtils.copyProperties(userDO, dto);
                    return dto;
                })
                .toList();
        return success(userRespDTOS);
    }

    @Override
    public List<AdminUserRespDTO> getUserList() {
        List<UserDO> userDOS = userMapper.selectListByStatus(CommonStatusEnum.NORMAL.getStatus());
        List<AdminUserRespDTO> userRespDTOS = userDOS.stream()
                .map(userDO -> {
                    AdminUserRespDTO dto = new AdminUserRespDTO();
                    BeanUtils.copyProperties(dto, userDO);
                    return dto;
                })
                .toList();
        return userRespDTOS;
    }

    @Override
    public void validateUserList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            throw new IllegalArgumentException("用户 ID 不能为空");
        }

    }
}
